package com.lesson11.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileController {
	
	//如果不写POST和GET都支持，否则如果写的话一定要写method={RequestMethod.POST}
	@RequestMapping(value={"/file/upload"},method={RequestMethod.POST})
	public Map<String, Object> upload(@RequestParam(name="file") MultipartFile file){
		InputStream in = null;
		FileOutputStream out = null;
		try {
			System.out.println("------------------"+file.getContentType());
			in = file.getInputStream();
			//上传文件的原文件名
			String fileName = file.getOriginalFilename();
			 out = new FileOutputStream(new File("d:/"+fileName));
			 int len = -1;
			 byte[] buffer = new byte[1024];
			 //循环读取文件输入流
			while((len = in.read(buffer))!=-1){
				out.write(buffer, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}
		
		 Map<String, Object> results = new HashMap<String, Object>();
		 results.put("code", "000000");
		 results.put("msg", "success");
		 return results;
	}
	
	
	//如果不写POST和GET都支持，否则如果写的话一定要写method={RequestMethod.POST}
		@RequestMapping(value={"/file/upload2"},method={RequestMethod.POST})
		public Map<String, Object> upload2(@RequestParam(name="file") MultipartFile file){
			InputStream in = null;
			FileOutputStream out = null;
			String contentType =file.getContentType();
			if(contentType!=null && "image/jpeg".equals(contentType.toLowerCase())){
			try {
				System.out.println("------------------"+file.getContentType());
				in = file.getInputStream();
				//上传文件的原文件名
				String fileName = file.getOriginalFilename();
				 out = new FileOutputStream(new File("d:/"+fileName));
				 int len = -1;
				 byte[] buffer = new byte[1024];
				 //循环读取文件输入流
				while((len = in.read(buffer))!=-1){
					out.write(buffer, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if(in!=null){
					try {
						in.close();
					} catch (IOException e) {
					}
				}
				
				if(out!=null){
					try {
						out.close();
					} catch (IOException e) {
					}
				}
			}
			
			 Map<String, Object> results = new HashMap<String, Object>();
			 results.put("code", "000000");
			 results.put("msg", "success");
			 return results;
			}
			Map<String, Object> results = new HashMap<String, Object>();
			 results.put("code", "000001");
			 results.put("msg", "fail");
			 return results;
		}
}
